/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getSetterList,
  getWxSetterList,
  getOperatorOrderSourceList,
  getYzList,
  editOrderSource,
  wsEditOrderSource,
  addOrderSource,
  wsAddOrderSource,
  getYzSetterList,
  yzEditOrderSource,
  getOrderChargeRecords,
} from '@/services/orderManagement';
import { isLegalData } from '@/utils/utils';
import { message } from 'antd';
import { isArray } from 'lodash';

export default {
  namespace: 'setter',

  state: {
    sourceType: '',
    yzList: [],
    setterList: {
      list: [],
      pagination: null,
    },
    selectData: null,
    wxSetterList: {
      list: [],
      pagination: null,
    },
    yzSetterList: {
      list: [],
      pagination: null,
    },
  },

  effects: {
    *getYzList({ payload }, { call, put }) {
      const response = yield call(getYzList, payload);
      if (!response) return;
      const { code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            yzList: data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            yzList: [],
          },
        });
      }
    },
    *getSetterList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSetterList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, page, totalCount, pageSize } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            setterList: {
              list: isArray(list) ? list : [],
              pagination: {
                total: totalCount,
                pageSize,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

    *getOperatorOrderSourceList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOperatorOrderSourceList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      for (const key in data) {
        if (data.hasOwnProperty(key)) {
          const element = data[key];
          if (key == 'sto44' && data.sto) {
            Object.assign(data.sto, data[key]);
            delete data[key];
          } else if (key == 'sto44' && !data.sto) {
            data.sto = data.sto44;
            delete data.sto44;
          }
          for (const _key in element) {
            if (element.hasOwnProperty(_key)) {
              const arr = element[_key];
              for (let i = 0; i < arr.length; i++) {
                const item = arr[i];
                const { branch_code, auth_id } = item;
                // 下拉框数据的唯一id
                item.u_id = branch_code ? `${_key}_${branch_code}` : `${_key}_${auth_id}`;
              }
            }
          }
        }
      }
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            selectData: data,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号设置，添加单号设置
    *addOrderSource({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addOrderSource, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号设置，添加单号设置
    *wsAddOrderSource({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wsAddOrderSource, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号设置，编辑单号设置
    *editOrderSource({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editOrderSource, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 微商单号设置，编辑单号设置
    *wsEditOrderSource({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wsEditOrderSource, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getWxSetterList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWxSetterList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { list, page, totalCount, pageSize } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            wxSetterList: {
              list: isArray(list) ? list : [],
              pagination: {
                total: totalCount,
                pageSize,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 驿站单号源列表
    *getYzSetterList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getYzSetterList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { list, page, totalCount, pageSize } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            yzSetterList: {
              list: isArray(list) ? list : [],
              pagination: {
                total: totalCount,
                pageSize,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 驿站单号源设置
    *yzEditOrderSource({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(yzEditOrderSource, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 单号设置，单号充值记录
    *getOrderChargeRecords({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getOrderChargeRecords, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;

      if (code === 0 && data) {
        const { total, page, data: list = [] } = data;

        __dva_resolve({
          list: isLegalData(list),
          pagination: {
            total,
            pageSize: 15,
            current: page,
            showQuickJumper: false,
            showSizeChanger: false,
          },
        });
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
